﻿using UnityEngine;
using System.Collections;
using UnityEngine.Events;

public class Player : MonoBehaviour {

	
	public float force=250f;

	private bool death=false;
    private Rigidbody2D rigid;
    private Animator anim;
	// Use this for initialization
	void Start () {
        rigid = this.GetComponent<Rigidbody2D>();
        anim = this.GetComponent<Animator>();
    }
	
	// Update is called once per frame
	void Update () {
		if (death)
			return;
		if (Input.GetMouseButtonDown(0)||(Input.touchCount > 0 &&Input.GetTouch(0).phase==TouchPhase.Began))
		{
			rigid.velocity=Vector2.zero;
            rigid.AddForce (new Vector2(0,force));
		}
	}


	public void Fly()
	{
        rigid.simulated = true;
        anim.SetTrigger("fly");
	}

	public void Die()
	{
		anim.SetTrigger("die");
		death = true;
        PipeLinesManager.Instance.StopPipe();
        game.Instance.Status = game.Game_Status.GameOver;
        event_manager.Instance.dispatch_event("best", null);
	}

	void OnTriggerEnter2D(Collider2D other)
	{
		if (other.gameObject.name == "ScoreArea") 
		{
            event_manager.Instance.dispatch_event("score", 1);
        }
	}

	void OnCollisionEnter2D(Collision2D other)
	{
		Die ();
	}
	
}
